#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ(2.59)
AC_INIT(WebGameServers, 0.1, adou1127@hotmail.com)
#AC_CONFIG_SRCDIR([include])

AM_CPPFLAGS="-I/usr/include -I/usr/local/include" 
AC_SUBST(AM_CPPFLAGS)
#CXXFLAGS=" -g -O0 -Wno-deprecated -std=c++0x"
CXXFLAGS=" -g -O0 -Wno-deprecated -fno-omit-frame-pointer -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free "
LDFLAGS="-L/usr/lib -L/usr/local/lib"
AC_SUBST(LDFLAGS)
#AM_INIT_AUTOMAKE([-Wall -Werror foreign])
AM_INIT_AUTOMAKE([-Wall foreign])


# Checks for programs.
AC_PROG_CXX
AC_PROG_CC

# autoheader templates
AH_TEMPLATE([USE_EPOLL], Define if you want to use the epoll socket engine module.)
AH_TEMPLATE([USE_KQUEUE], Define if you want to use the kqueue socket engine module.)
AH_TEMPLATE([HAVE_DARWIN], Define if you have MacOSX Darwin, to include the correct headers for kevent)
AH_TEMPLATE([ENABLE_IMPROVED_TICKETSYSTEM], Define if you want to enable the improved ticket system. Warning: Not My_Master compatib
le!)
AH_TEMPLATE([USING_BIG_ENDIAN], Define this if you are on a big-endian system.)
AH_TEMPLATE([USING_LITTLE_ENDIAN], Define this if you are on a non-big-endian system.)
AH_TEMPLATE([X64], Define this if you are under an X64-based operating system.)
AH_TEMPLATE([X86], Define this if you are under an X86-based 32bit operating system.)
AH_TEMPLATE([NO_DBLAYER_MYSQL], Define this if you want to disable mysql support.)


# Default prefix
AC_PREFIX_DEFAULT(`/bin/pwd`)
test "$prefix" = "NONE" && prefix=`pwd`
test "$sysconfdir" = "\${prefix}/etc" && sysconfdir='../etc'
#test "$scriptdir" = "" && scriptdir="\${prefix}/scripts"
#test "$scriptsdir" = "" && scriptsdir="../scripts"
test "$scriptsdir" = "" && scriptsdir="\${prefix}/scripts"
AC_SUBST(scriptsdir,"$scriptsdir")
test "$sharddir" = "" && sharddir="\${prefix}/shard"
AC_SUBST(sharddir,"$sharddir")

# Determine the operating system
AC_MSG_CHECKING([uname -s for detecting host operating system])
OSNAME=`uname -s`
case "$OSNAME" in
        FreeBSD*)
                dnl FreeBSD Owns !
                AC_MSG_RESULT(FreeBSD Owns your hd)
                FREEBSD=yes
        ;;
        Linux*)
                dnl Linux...
                AC_MSG_RESULT(Linux)
                LINUX=yes
        ;;
        Darwin*)
                dnl Darwin owns to (includes BSD) :P
                AC_MSG_RESULT(Darwin)
                FREEBSD=yes
        ;;
        *)
                AC_MSG_RESULT(no)
        ;;
esac

AC_MSG_CHECKING([uname -m for detecting host operating system architecture])
ARCH=`uname -m`
case "$ARCH" in
        amd64*)
                dnl yay 21st century hardware
                AC_MSG_RESULT(x64 feel teh power)
                AC_DEFINE(X64)
                AC_SUBST(ARCH,x64)
        ;;
        x86_64*)
                dnl yay 21st century hardware
                AC_MSG_RESULT(x64 feel teh power)
                AC_DEFINE(X64)
                AC_SUBST(ARCH,x64)
        ;;
        *)
                AC_MSG_RESULT(x86 oh noes i ran out of address space)
                AC_DEFINE(X86)
                AC_SUBST(ARCH,x86)
        ;;
esac

AC_C_BIGENDIAN([AC_DEFINE(USING_BIG_ENDIAN)],[AC_DEFINE(USING_LITTLE_ENDIAN)],[]
)



# Checks for libraries.
# Reset 

CFLAGS="$CFLAGS $INCLUDES -D_REENTRANT -Wall -fno-strict-aliasing"
CXXFLAGS="$CXXFLAGS $INCLUDES -D_REENTRANT -Wall -fno-strict-aliasing"

# Check for c++0x functionnality (unordered_map)

if test "$ac_cv_cxx_compile_cxx0x_gxx" = yes; then
        CXXFLAGS="$CXXFLAGS -std=gnu++0x"
else
        if test "$ac_cv_cxx_compile_cxx0x_cxx" = yes; then
                CXXFLAGS="$CXXFLAGS -std=c++0x"
        fi
fi


# Check for pthread

AC_PROG_RANLIB
#AM_PROG_AR


AC_ARG_ENABLE(debug,
     AC_HELP_STRING([--enable-debug],[enable debug]),
         [enable_debug=yes],
         [enable_debug=no]
)

if test "x$enable_debug" = "xyes" ; then
        CFLAGS="$CFLAGS -g -O0 -D__DEBUG__"
        CXXFLAGS="$CXXFLAGS -g -O0 -D__DEBUG__"
fi

AC_ARG_ENABLE(optimize,
     AC_HELP_STRING([--enable-optimize],[enable code optimization]),
         [enable_optimize=yes],
         [enable_optimize=no]
)

if test "x$enable_optimize" = "xyes" ; then
        CFLAGS="$CFLAGS -O2 -pipe"
        CXXFLAGS="$CXXFLAGS -O2 -pipe"
        if test "x$enable_debug" != "xyes" ; then
                CFLAGS="$CFLAGS -fomit-frame-pointer"
                CXXFLAGS="$CXXFLAGS -fomit-frame-pointer"
        fi
fi


# Check for networking stuff
AC_MSG_CHECKING([detecting network socket engine])
if test "x$OSNAME" = "xLinux" ; then
        AC_MSG_RESULT(epoll)
        AC_CHECK_HEADERS(sys/epoll.h)
        AC_DEFINE(USE_EPOLL)
        AC_SUBST(NETWORKING, "epoll (Linux)")
fi

if test "x$OSNAME" = "xFreeBSD" ; then
        AC_MSG_RESULT(kqueue)
        AC_CHECK_FUNC(kevent, , AC_MSG_ERROR([Cannot find kevent() function for socket engine]))
        AC_CHECK_HEADER(sys/event.h, , AC_MSG_ERROR([Cannot find sys/event.h for socket engine]))
        AC_DEFINE(USE_KQUEUE)
        AC_SUBST(NETWORKING, "kqueue (FreeBSD)")
fi

if test "x$OSNAME" = "xDarwin" ; then
        AC_MSG_RESULT(kqueue)
        AC_CHECK_FUNC(kevent, , AC_MSG_ERROR([Cannot find kevent() function for socket engine]))
        AC_CHECK_HEADER(sys/event.h, , AC_MSG_ERROR([Cannot find sys/event.h for socket engine]))
        AC_DEFINE(USE_KQUEUE)
        AC_SUBST(NETWORKING, "kqueue (Darwin)")

        INCLUDES+="-I/sw/include -I/sw/include/mysql -I/usr/include/openssl"
        LDFLAGS+="-L/sw/lib -L/sw/lib/mysql -L/usr/lib"
fi

# Check for mysql
#MYSQL_FOUND=AX_LIB_MYSQL([5.0])
#if test "$MYSQL_VERSION" = ""; then
#        AC_MSG_ERROR([*** mysql client library not found. Please install it.])
#else
#        LDFLAGS="$LDFLAGS $MYSQL_LDFLAGS"
#        CFLAGS="$CFLAGS $MYSQL_CFLAGS"
#        CXXFLAGS="$CXXFLAGS $MYSQL_CFLAGS"
#	#AC_SUBST(ENABLE_DATABASE_MYSQL,"1")
#fi
LDFLAGS+=" -rdynamic -L/usr/lib/mysql -lmysqlclient_r -lz -lpthread -lcrypt -lnsl -lm -lpthread -L/usr/lib -lssl -lcrypto"
CFLAGS+=" -I/usr/include/mysql "
CXXFLAGS+=" -I/usr/include/mysql "

#check for boost threading lib

# Checks for header files.
AC_HEADER_STDC
AC_CHECK_HEADERS([arpa/inet.h netdb.h netinet/in.h stdint.h stdlib.h string.h sys/ioctl.h sys/socket.h sys/time.h sys/timeb.h unistd.h])

# Checks for typedefs, structures, and compiler characteristics.
AC_HEADER_STDBOOL
AC_C_CONST
AC_C_INLINE
AC_TYPE_SIZE_T
AC_HEADER_TIME
AC_STRUCT_TM
AC_PROG_LIBTOOL

# Checks for library functions.
#AC_FUNC_MALLOC
AC_FUNC_MEMCMP
AC_FUNC_MKTIME
AC_CHECK_FUNCS([gettimeofday memset])



AC_DEFINE_UNQUOTED([PREFIX], "${prefix}", "Prefix")
AC_DEFINE_UNQUOTED([SCRIPTSDIR], "${scriptsdir}", "librarydir")
AC_DEFINE_UNQUOTED([CONFDIR], "${sysconfdir}", "configdir")
AC_DEFINE_UNQUOTED([SHARDDIR], "${sharddir}", "LibShard")


AC_CONFIG_FILES([Makefile
                 src/interface/Makefile
                 src/world_scripts/Makefile
                 src/battle_scripts/Makefile])

AC_OUTPUT
echo
echo "===================================="
echo "WebGameServer configure report:"
echo
echo " Core:"
echo "             os: ${OSNAME}"
echo "         prefix: ${prefix}"
echo "   architecture: ${ARCH}"
echo "===================================="
echo " Database modules:"
echo "        mysql: ${MYSQL_VERSION}"
if test "x$enable_postgres" = "xyes" ; then
  echo "   postgresql: ${POSTGRESQL_VERSION}"
fi
if test "x$enable_sqlite" = "xyes" ; then
  echo "       sqlite: ${SQLITE_VERSION}"
fi

echo
echo " Networking:"
echo "   ${NETWORKING}"
echo
echo " Features: "


echo -n "    improved ticket system: "
if test "x$enable_ticketsystemex" = "xyes" ; then
  echo "enabled"
else
  echo "disabled"
fi

echo
echo "Compiler version: "
$($CC -v)
echo
echo "LIBS=${LIBS}"
echo "CXXFLAGS=${CXXFLAGS}"
echo "LDFLAGS=${LDFLAGS}"
#echo "DEFS=${DEFS}"
echo "===================================="
echo
